﻿namespace ClassyBlog.DataAccess.Specs.Unit
{
    using System;

    using Machine.Specifications;
    using NSubstitute;

    using DomainObjects;
    using Infrastructure;

    [Subject(typeof(BlogRepository))]
    public class when_creating_blog_repository_with_blank_data_folder
    {
        static Exception exception;

        Because of = () => exception =
            Catch.Exception(() =>
                new BlogRepository(
                    string.Empty, Substitute.For<IFileSystem>()));

        It should_throw_exception = () => exception.ShouldBeOfType<ArgumentException>();
    }

    [Subject(typeof(BlogRepository))]
    public class when_creating_blog_repository_with_null_file_system
    {
        static Exception exception;

        Because of = () => exception =
            Catch.Exception(() =>
                new BlogRepository(
                    @"c:\inetpub\wwwroot\classyblog\app_data", null));

        It should_throw_exception = () =>
            exception.ShouldBeOfType<ArgumentNullException>();
    }

    [Subject(typeof(BlogRepository))]
    public class when_retrieving_blog
    {
        const string DataDirectory = @"c:\inetpub\wwwroot\classyblog\app_data";
        const string Title = "Kazi Manzur Rashid";
        const string Tagline = "Sharing Thoughts and Learning";
        const string MetaKeywords =
            ".NET, ASP.NET, C#, Linq, Microformat, Html5, Css3, jQuery, " +
            "Ruby, REST, Agile, ORM, TDD, BDD, DDD, Usability, " +
            "Developer, Bangladesh";

        const string MetaDescription = "Personal blog of Kazi Manzur Rashid";
        const string Author = "Kazi Manzur Rashid";
        const string Email = "kazimanzurrashid@gmail.com";
        const string Theme = "Magic";
        const int PagesPerPage = 10;
        const int PostsPerPage = 15;

        static IFileSystem fileSystem;
        static IBlogRepository repository;
        static Blog blog;

        Establish context = () =>
        {
            var newLine = Environment.NewLine;

            var content = "Title: " + Title + newLine +
                          "Tagline: " + Tagline + newLine +
                          "MetaKeywords: " + MetaKeywords + newLine +
                          "MetaDescription: " + MetaDescription + newLine +
                          "Author: " + Author + newLine +
                          "Email: " + Email + newLine +
                          "PagesPerPage: " + PagesPerPage + newLine +
                          "PostsPerPage: " + PostsPerPage + newLine +
                          "Theme: " + Theme + newLine +
                          "Foo: Bar";

            fileSystem = Substitute.For<IFileSystem>();
            fileSystem.ReadAll(Arg.Any<string>()).Returns(content);

            repository = new BlogRepository(DataDirectory, fileSystem);
        };

        Because of = () => blog = repository.One();

        It should_retrieve_the_blog = () =>
        {
            blog.Title.ShouldEqual(Title);
            blog.Tagline.ShouldEqual(Tagline);
            blog.MetaKeywords.ShouldEqual(MetaKeywords);
            blog.MetaDescription.ShouldEqual(MetaDescription);
            blog.Author.ShouldEqual(Author);
            blog.Email.ShouldEqual(Email);
            blog.PagesPerPage.ShouldEqual(PagesPerPage);
            blog.PostsPerPage.ShouldEqual(PostsPerPage);
            blog.Theme.ShouldEqual(Theme);
            blog.GetDynamicProperty("Foo").ShouldEqual("Bar");
        };
    }
}